var userlist;
function $(id) {
	return document.getElementById(id);
}
function createXHR() {
	var XHR = [
		function () { return new XMLHttpRequest(); },
		function () { return new ActiveXObject("Msxml2.XMLHTTP"); },
		function () { return new ActiveXObject("Msxml3.XMLHTTP"); },
		function () { return new ActiveXObject("Microsoft.XMLHTTP"); },
	];
	var hxr = null;
	for (var i = 0; i < 4; i++) { try { hxr = XHR[i](); } catch (e) { continue; } break; }
	return hxr;
}
window.onload = function () {
	if (localStorage.getItem("regist-page-type") == null)
		localStorage.setItem("regist-page-type", "login");
	if (localStorage.getItem("regist-page-type") == "login") {
		$("login").style.visibility = "visible";
		$("register").style.visibility = "hidden";
	} else if (localStorage.getItem("regist-page-type") == "register") {
		$("login").style.visibility = "hidden";
		$("register").style.visibility = "visible";
	}
	//page页面状态调整完毕！
	//初始化登录信息
	if (localStorage.getItem("user") == undefined) {//判断是否为初次登录
		var request = createXHR();//创建XHR对象
		request.onreadystatechange = function () {
			if (request.readyState == 4 && request.status == 200) {//判断是否取数据完毕
				userlist = JSON.parse(request.response);/*uid有8位，password有10位*/
				if (userlist == null || userlist == undefined) alert("用户数据加载失败！！！");
				localStorage.setItem("user", JSON.stringify(userlist));//储存userlist对象
			}
		};
		request.open("GET", "json/userlist.json", true);
		request.setRequestHeader("Accept", "application/json");
		request.send();
	}
	else userlist = JSON.parse(localStorage.getItem("user"));
};
function setLogin() {//使得login页面显示
	$("login").style.visibility = "visible";
	$("register").style.visibility = "hidden";
}
function setRegister() {//使得register页面显示
	$("login").style.visibility = "hidden";
	$("register").style.visibility = "visible";
}
function check_login() {//检查login是否合适
	var id = $("login-id").value;
	var password = $("login-password").value;
	var name = "";
	var tip = "";
	if (id.length != 8) tip += "账号长度为8位！\n";
	if (isNaN(Number(id))) tip += "账号必须为全数字组成！\n";
	if (password.length != 10) tip += "密码长度必须为10位！\n";
	for (var i = 0; i < userlist.length; i++)
		if (userlist[i].uid == id && userlist[i].password == password) {
			name = userlist[i].name;
			break;
		}
	if (name == "") tip += "账号或密码错误！！！";
	if (tip != "") alert(tip);
	else success(id, name, password);
	return false;
}
function check_register() {//检查
	var id = $("register-id").value;
	var password = $("register-password").value;
	var username = $("register-username").value;
	var repeat_password = $("register-repeat").value;
	var tip = "";
	if (id.length != 8) tip += "账号长度为8位！\n";
	if (isNaN(Number(id))) tip += "账号必须为全数字组成！\n";
	if (password.length != 10) tip += "密码长度必须为10位！\n";
	if (password != repeat_password) tip += "两次输入密码不同！\n";
	if (username == "") tip += "用户名不能为空！\n";
	for (var i in userlist) {
		if (userlist[i].uid == id) {
			tip += "此账号已存在，请更换账号！！！\n";
			break;
		}
	}
	if (tip != "") alert(tip);
	else {
		userlist.push({
			"uid": id,
			"name": username,
			"password": password
		});
		localStorage.setItem("user", JSON.stringify(userlist));
		success(id, username, password);
	}
	return false;
}

function success(id, username, password) {
	localStorage.setItem("id", id);
	localStorage.setItem("username", username);
	localStorage.setItem("password", password);
	localStorage.setItem("login_time", Number(new Date()));//设置登录时间
	window.open("index.html");
}